package com.caac.core.web.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.caac.core.dm.ViewProp;
import com.caac.core.util.DateUtils;
import com.caac.core.web.form.BaseForm;
import com.caac.core.web.util.LabelColorBean;
import com.caac.core.web.util.LabelLinkBean;
import com.caac.core.web.util.LabelLinkBean2;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2004</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public abstract class BaseExportAction extends BaseAction {
    protected final ActionForward doExecute(ActionMapping mapping,
                                            BaseForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response) throws
            Exception {
        ViewForward vf = doViewExecute(mapping, form, request, response);
        if (vf.getViewProps() == null) {
            return new ActionForward("/pages/exportError.jsp");
        }
        response.reset();
        response.setHeader("Content-disposition",
                           "attachment;filename=export.xls");
        response.setContentType("application/vnd.ms-excel;charset=GBK");
        WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream());
        WritableSheet ws = wwb.createSheet("Sheet1", 0);
        List dmList = (List) vf.getData();
        List pList = vf.getViewProps();

        WritableFont wFont = new WritableFont(WritableFont.ARIAL, 10,
                                              WritableFont.BOLD);
        WritableCellFormat wcf = new WritableCellFormat(wFont);
        wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
        wcf.setVerticalAlignment(jxl.format.VerticalAlignment.
                                 CENTRE);

        WritableFont wFont2 = new WritableFont(WritableFont.ARIAL, 8);
        WritableCellFormat wcf2 = new WritableCellFormat(wFont2);
        wcf2.setBorder(Border.ALL, BorderLineStyle.THIN);
        wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.
                                  CENTRE);
        WritableFont nf = new WritableFont(WritableFont.ARIAL, 8);
        WritableCellFormat numCF = new WritableCellFormat(nf);
        numCF.setAlignment(jxl.format.Alignment.RIGHT);
        numCF.setVerticalAlignment(jxl.format.VerticalAlignment.
                                   CENTRE);
        numCF.setWrap(true);
        numCF.setBorder(Border.ALL, BorderLineStyle.THIN);
        if (pList != null) {
            for (int iCol = 0; iCol < pList.size(); iCol++) {
                ViewProp vpDm = (ViewProp) pList.get(iCol);
                Label labelC = new Label(iCol, 0, vpDm.getLabel(), wcf);
                ws.addCell(labelC);
            }
        }
        try {
            for (int iRow = 0; iRow < dmList.size(); iRow++) {
                BeanWrapper bw = new BeanWrapperImpl(dmList.get(iRow));
                for (int iCol = 0; iCol < pList.size(); iCol++) {
                    ViewProp vp = (ViewProp) pList.get(iCol);
                    String propName = vp.getPropName();
                    String value = "";
                    double value1 = 0.00;
                    if ((bw.getPropertyType(propName).getName().equals(
                            "java.lang.String"))) {
                        value = (String) bw.getPropertyValue(propName);
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "java.util.Date")) {
                        value = DateUtils.convertToString("yyyy-MM-dd HH:mm",
                                (Date) bw.getPropertyValue(propName));
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "java.lang.Integer")) {
                        if (bw.getPropertyValue(propName) != null) {
                            value1 = new Double(String.valueOf(bw.
                                    getPropertyValue(
                                            propName))).doubleValue();
                        }
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "java.math.BigDecimal")) {
                        if (bw.getPropertyValue(propName) != null) {
                            value1 = new Double(String.valueOf(bw.
                                    getPropertyValue(
                                            propName))).doubleValue();
                        }
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "java.lang.Double")) {
                        if (bw.getPropertyValue(propName) != null) {
                            value1 = new Double(String.valueOf(bw.
                                    getPropertyValue(
                                            propName))).doubleValue();
                        }
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "com.caac.core.web.util.LabelColorBean")) {
                        value = ((LabelColorBean) bw.getPropertyValue(propName)).
                                getLabel();
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "com.caac.core.web.util.LabelLinkBean")) {
                        value = ((LabelLinkBean) bw.getPropertyValue(propName)).
                                getLabel();
                    }else if (bw.getPropertyType(propName).getName().equals(
                        "com.caac.core.web.util.LabelLinkBean2")) {
                    	value = ((LabelLinkBean2) bw.getPropertyValue(propName)).
                            getLabel();
                    } else if (bw.getPropertyType(propName).getName().equals(
                            "int")) {
                        value = bw.getPropertyValue(propName) + "";
                    }

                    if (bw.getPropertyType(propName).getName().equals(
                            "java.lang.Integer") ||
                        bw.getPropertyType(propName).getName().equals(
                                "java.math.BigDecimal") ||
                        bw.getPropertyType(propName).getName().equals(
                                "java.lang.Double") ||
                        bw.getPropertyType(propName).getName().equals(
                                "int")) {
                        ws.addCell(new Number(iCol, iRow + 1, value1, numCF));
                    } else {
                        Label labelC = new Label(iCol, iRow + 1, value, wcf2);
                        ws.addCell(labelC);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        wwb.write();
        wwb.close();
        return null;
    }

    protected abstract ViewForward doViewExecute(ActionMapping mapping,
                                                 BaseForm form,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) throws
            Exception;
}
